package com.why.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.why.servicebase.exception.GuilException;
import com.why.usercenter.bean.UcenterMember;
import com.why.usercenter.bean.vo.LoginVo;
import com.why.usercenter.bean.vo.RegisterVo;
import com.why.usercenter.mapper.UcenterMemberMapper;
import com.why.usercenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.why.utils.JwtUtils;
import com.why.utils.MD5;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author why
 * @since 2021-02-15
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    RedisTemplate<String, String> redis;

    /**
     * 完成用户登录功能
     *
     * @param member
     * @return
     */
    @Override
    public String loginUser(LoginVo member) {
        String mobile = member.getMobile();
        String password = member.getPassword();
        System.out.println(member + "\t" + password);
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuilException(20001, "用户名或密码不能为空！");
        }
        //检查用户名
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile", mobile);
        UcenterMember one = baseMapper.selectOne(wrapper);
        System.out.println("用户的信息为:" + one);
       //判断用户信息是否为空
        if (one == null) {
            throw new GuilException(20001, "用户名不存在！");
        }
        //检查登录密码
        //数据库中不存明文密码
        //使用md5完成加密,MD5无解密
        System.out.println("加密前的密码:"+password);
        password= MD5.encrypt(password);
        System.out.println("加密后的密码:"+password);
        if (!password.equals(one.getPassword())) {
            throw new GuilException(20001, "密码错误请检查！");
        }

        //判断用户是否被封号
        if (one.getIsDisabled()) {
            throw new GuilException(20001, "账号涉嫌违规已被禁用！");
        }
        //登录成功，根据从数据库中查询到的数据生成token
        String token = JwtUtils.getJwtToken(one.getId(), one.getNickname());
        System.out.println("token is==>" + token);
        return token;
    }

    /**
     * 完成用户注册功能
     * @param register
     * @return
     */
    @Override
    public String registerUser(RegisterVo register) {
        //得到手机输入的验证码
        String code = register.getCode();
        //得到手机号
        String mobile = register.getMobile();
        //得到用户昵称
        String nickname = register.getNickname();
        //得到用户密码
        String password = register.getPassword();
        if(StringUtils.isEmpty(code)||StringUtils.isEmpty(mobile)
          ||StringUtils.isEmpty(nickname)||StringUtils.isEmpty(password)){
            throw new GuilException(20001,"有东西为空，请检查！");
        }

        //判断验证码，从redis中获取
        String redisCode = redis.opsForValue().get(mobile);
        if(redisCode==null){
            throw new GuilException(20001,"验证码过期！");
        }
        if(!redisCode.equals(code)){
            throw  new GuilException(20001,"验证码错误！");
        }

        //判断手机号是否重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        //判断表中有无数据
        Integer count = baseMapper.selectCount(wrapper);
        if(count>0){
            throw  new GuilException(20001,"该手机号已经存在！");
        }
        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));
        //用户禁用
        member.setIsDisabled(false);
        //设置默认头像
        member.setAvatar("https://why741.xyz/upload/avatar.jpg");
        baseMapper.insert(member);

        return null;
    }

    @Override
    public UcenterMember getMemberByOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public Integer countRegisterByDay(String day) {

        return  baseMapper.selectRegisterCount(day);
    }


}
